<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace api\log\controller;
use cmf\controller\RestAdminBaseController;
use api\mall\model\StoreModel;
use cmf\controller\RestBaseController;
use think\facade\Db;


/**
 * Description of LoggerController
 *
 * @author PC
 */
class LoggerController extends RestBaseController {
    //put your code here
    public function index(){
        $list = Db::name('log')->order('login_dt desc')->limit(10)->select();
        $this->success('ok',$list);
    }

    public function daily(){
        $list = Db::name('log_daily')->order('date desc')->limit(10)->select();
        $this->success('ok',$list);
    }

    public function date(){
        $date = $this->request->get('date');
        if(empty($date)){
            $date = date('Y-m-d');
        }
        $list = Db::name('log')->where('login_dt','>',$date.' 00:00:00')->limit(1000)->select();
        $this->success('ok',$list);
    }


    public function task_daily(){
        $date = $this->request->get('date');
        if(empty($date)){
            $date = date('Y-m-d');
        }
        Db::name('log_daily')->where('date',$date)->delete();
        $time0 = $date.' 00:00:00';
        $time1 = $date.' 23:59:59';
        $uid = Db::name('log')->whereBetweenTime('login_dt',$time0,$time1)->group('user_id')->count();
        $uips = Db::name('log')->whereBetweenTime('login_dt',$time0,$time1)->group('login_ip')->count();
        Db::name('log_daily')->insert([
            'date'=>$date,
            'user_num'=>$uid,
            'ip_num'=>$uips,
            'create_time'=>time()
        ]);
        $this->success('ok',$date);
    }
}
